package com.example.zyfypt_206shj.activity;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.example.zyfypt_206shj.R;
import com.example.zyfypt_206shj.bean.SessionID;
import com.example.zyfypt_206shj.iface.GuanzhuListener;
import com.example.zyfypt_206shj.iface.IsGuanzhuListener;
import com.example.zyfypt_206shj.iface.IsShoucangListener;
import com.example.zyfypt_206shj.iface.ShoucangListener;
import com.example.zyfypt_206shj.iface.unGuanzhuListener;
import com.example.zyfypt_206shj.iface.unShoucangListener;
import com.example.zyfypt_206shj.model.GuanzhuModel;
import com.example.zyfypt_206shj.model.IsGuanzhuModel;
import com.example.zyfypt_206shj.model.IsShoucangModel;
import com.example.zyfypt_206shj.model.ShoucangModel;
import com.example.zyfypt_206shj.model.unGuanzhuModel;
import com.example.zyfypt_206shj.model.unShoucangModel;
import com.example.zyfypt_206shj.service.GuanzhuService;

public class activity_content extends AppCompatActivity {

    private WebView webView;
    private boolean isShoucang;
    private boolean isGuanzhu;
    private IsGuanzhuModel isGuanzhuModel;
    private IsShoucangModel isShoucangModel;
    private ShoucangModel shoucangModel;
    private GuanzhuModel guanzhuModel;
    private unShoucangModel unshoucangModel;
    private unGuanzhuModel unguanzhuModel;
    private MenuItem menu_shoucang = null,menu_guanzhu = null;
    private Intent intent_uper;

    private IsGuanzhuListener isGuanzhuListener = new IsGuanzhuListener() {
        @Override
        public void onResponse(String result) {
            if(result.equals("1")){
                menu_guanzhu.setTitle("未关注");
                isGuanzhu = false;
            }else if(result.equals("0")){
                menu_guanzhu.setTitle("已关注");
                isGuanzhu = true;
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(activity_content.this, "fail:", Toast.LENGTH_SHORT).show();
        }
    };

    private IsShoucangListener isShoucangListener = new IsShoucangListener() {
        @Override
        public void onResponse(String result) {
            if(result.equals("1")){
                menu_shoucang.setTitle("未收藏");
                isShoucang = false;
            }else if(result.equals("0")){
                menu_shoucang.setTitle("已收藏");
                isShoucang = true;
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(activity_content.this, "fail:" + msg, Toast.LENGTH_SHORT).show();
        }
    };

    private ShoucangListener shoucangListener = new ShoucangListener() {
        @Override
        public void onResponse(String result) {
            if(result.equals("0")){
                Toast.makeText(activity_content.this, "收藏失败", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(activity_content.this, "收藏成功", Toast.LENGTH_SHORT).show();
                menu_shoucang.setTitle("已收藏");
                isShoucang = true;
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(activity_content.this, "fail:" + msg, Toast.LENGTH_SHORT).show();
        }
    };

    private unShoucangListener unshoucangListener = new unShoucangListener() {
        @Override
        public void onResponse(String result) {
            if(result.equals("1")){
                Toast.makeText(activity_content.this, "取消收藏成功", Toast.LENGTH_SHORT).show();
                menu_shoucang.setTitle("未收藏");
                isShoucang = false;
            }else if(result.equals("0")){
                Toast.makeText(activity_content.this, "取消收藏失败", Toast.LENGTH_SHORT).show();
                menu_shoucang.setTitle("未收藏");
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(activity_content.this, "fail:" + msg, Toast.LENGTH_SHORT).show();
        }
    };

    private GuanzhuListener guanzhuListener = new GuanzhuListener() {
        @Override
        public void onResponse(String result) {
            if(result.equals("0")){
                Toast.makeText(activity_content.this, "关注失败", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(activity_content.this, "关注成功", Toast.LENGTH_SHORT).show();
                menu_guanzhu.setTitle("已关注");
                isGuanzhu = true;
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(activity_content.this, "fail:" + msg, Toast.LENGTH_SHORT).show();
        }
    };

    private unGuanzhuListener unguanzhuListener = new unGuanzhuListener() {
        @Override
        public void onResponse(String result) {
            if(result.equals("1")){
                Toast.makeText(activity_content.this, "取消关注成功", Toast.LENGTH_SHORT).show();
                menu_guanzhu.setTitle("未关注");
                isGuanzhu = false;
            }else if(result.equals("0")){
                Toast.makeText(activity_content.this, "取消关注失败", Toast.LENGTH_SHORT).show();
                menu_guanzhu.setTitle("未关注");
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(activity_content.this, "fail:" + msg, Toast.LENGTH_SHORT).show();
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content);
        webView = (WebView) findViewById(R.id.WebView);
        intent_uper = getIntent();
        //判断是否收藏关注
        isGuanzhuModel = new IsGuanzhuModel();
        isGuanzhuModel.getResult("userfocus", Integer.parseInt(intent_uper.getStringExtra("userid")), isGuanzhuListener);
        isShoucangModel = new IsShoucangModel();
        isShoucangModel.getResult(intent_uper.getStringExtra("mod"), Integer.parseInt(intent_uper.getStringExtra("id")), isShoucangListener);

        webView.loadUrl("http://amicool.neusoft.edu.cn/"
                + intent_uper.getStringExtra("mod")
                + ".php/show/index/id/"
                + intent_uper.getStringExtra("id"));
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
    }


    /**
     * onCreateOptionsMenu将创建的菜单显示出来，onOptionsItemSelected设置菜单的响应事件
     * @param menu
     * @return
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.content_setting, menu);
        menu_guanzhu = menu.add(Menu.NONE,1,1,"关注");
        menu_guanzhu.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

        menu_shoucang = menu.add(Menu.NONE,2,2,"收藏");
        menu_shoucang.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case 1:
                if(isGuanzhu){
                    //正在关注，这里需要启动取消关注模块
                    unguanzhuModel = new unGuanzhuModel();
                    unguanzhuModel.getResult("userfocus", Integer.parseInt(intent_uper.getStringExtra("userid")), SessionID.sessionID, unguanzhuListener);
                }else {
                    guanzhuModel = new GuanzhuModel(this);
                    guanzhuModel.getResult("userfocus", Integer.parseInt(intent_uper.getStringExtra("userid")), SessionID.sessionID, guanzhuListener);
                }
//                Toast.makeText(this, "您已关注", Toast.LENGTH_SHORT).show();
                break;
            case 2:
                if(isShoucang){
                    //正在收藏，这里需要启动取消收藏模块
                    unshoucangModel = new unShoucangModel();
                    unshoucangModel.getResult(intent_uper.getStringExtra("mod"), Integer.parseInt(intent_uper.getStringExtra("id")), SessionID.sessionID, unshoucangListener);
                }else {
                    shoucangModel = new ShoucangModel();
                    shoucangModel.getResult(intent_uper.getStringExtra("mod"), Integer.parseInt(intent_uper.getStringExtra("id")), SessionID.sessionID, shoucangListener);
                }
//                Toast.makeText(this, "您已收藏", Toast.LENGTH_SHORT).show();
                break;
            default:
        }
        return true;
    }

}
